/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sms4j.modem.gateway;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.smslib.AGateway.Protocols;
import org.smslib.GatewayException;
import org.smslib.Library;
import org.smslib.SMSLibException;
import org.smslib.Service;
import org.smslib.TimeoutException;
import org.smslib.modem.SerialModemGateway;
import sms4j.config.Config;
import sms4j.notification.GatewayStatusNotification;
import sms4j.notification.CallNotification;
import sms4j.notification.InboundNotification;
import sms4j.notification.OrphanedMessageNotification;
import sms4j.notification.OutboundNotification;

/**
 *
 * @author Swang
 */
public class GsmModemGateway {

    private static final Log log = LogFactory.getLog(GsmModemGateway.class);
    // Create the notification callback method for inbound & status report messages.
    private static InboundNotification inboundNotification = new InboundNotification();
    // Create the notification callback method for inbound voice calls.
    private static CallNotification callNotification = new CallNotification();
    //Create the notification callback method for gateway statuses.
    private static GatewayStatusNotification statusNotification = new GatewayStatusNotification();
    private static OrphanedMessageNotification orphanedMessageNotification = new OrphanedMessageNotification();
    private static OutboundNotification outboundMessageNotification = new OutboundNotification();

    public static void init() {
        try {
            log.info("Init Gsm Modem Gateway");
            log.info("Base Library Desrc->" + Library.getLibraryDescription());
            log.info("Base Library Version->" + Library.getLibraryVersion());
            
            //通过串口连接的GSM Modem
            SerialModemGateway gateway = new SerialModemGateway(Config.GSM_MODEM_GATEWAY_ID, Config.GSM_MODEM_GATEWAY_COM_PROT,
                    Config.GSM_MODEM_GATEWAY_BAUD_RATE, Config.GSM_MODEM_GATEWAY_MANUFACTURER, Config.GSM_MODEM_GATEWAY_MODEL);
            // Set the modem protocol to PDU (alternative is TEXT). PDU is the default, anyway...
            gateway.setProtocol(Protocols.PDU);
            // Do we want the Gateway to be used for Inbound messages?
            gateway.setInbound(false);
            // Do we want the Gateway to be used for Outbound messages?
            gateway.setOutbound(true);
            // Let SMSLib know which is the SIM PIN.
            gateway.setSimPin("0000");

            // Set up the notification methods.
            Service.getInstance().setInboundMessageNotification(inboundNotification);
            Service.getInstance().setCallNotification(callNotification);
            Service.getInstance().setGatewayStatusNotification(statusNotification);
            Service.getInstance().setOrphanedMessageNotification(orphanedMessageNotification);
            Service.getInstance().setOutboundMessageNotification(outboundMessageNotification);
            // Add the Gateway to the Service object.
            Service.getInstance().addGateway(gateway);
            // Similarly, you may define as many Gateway objects, representing
            // various GSM modems, add them in the Service object and control all of them.
            // Start! (i.e. connect to all defined Gateways)
            Service.getInstance().startService();
            // Printout some general information about the modem.
            log.info("Modem Information:");
            log.info("Manufacturer->" + gateway.getManufacturer());
            log.info("Model->" + gateway.getModel());
            log.info("Serial No->" + gateway.getSerialNo());
        } catch (TimeoutException ex) {
            log.error("Error init gateway: ", ex);
        } catch (IOException ex) {
            log.error("Error init gateway: ", ex);
        } catch (InterruptedException ex) {
            log.error("Error init gateway: ", ex);
        } catch (GatewayException ex) {
            log.error("Error init gateway: ", ex);
        } catch (SMSLibException ex) {
            log.error("Error init gateway: ", ex);
        }
    }

    public static void shutdown() {
        try {
            Service.getInstance().stopService();
        } catch (TimeoutException ex) {
            log.error("Error shutdown gateway: ", ex);
        } catch (GatewayException ex) {
            log.error("Error shutdown gateway: ", ex);
        } catch (SMSLibException ex) {
            log.error("Error shutdown gateway: ", ex);
        } catch (IOException ex) {
            log.error("Error shutdown gateway: ", ex);
        } catch (InterruptedException ex) {
            log.error("Error shutdown gateway: ", ex);
        }
    }
}
